// pages/home/home.js
Page({
    /**
     * 页面的初始数据
     */
    data: {
        // 轮播图的图片
        swiperImage:[],
        // 今天的日期
        today:1,
        // 推荐歌单
        playlist:[],
        // 新碟上架
        newalbum:[],
        // 榜单数据
        toplist:[],
        // 当前显示到那张图片
        listIndex1:3,
        listIndex2:3,
    },
    // 获取轮播图信息
    async getSwiperImage(){
        const res = await wx.p.request({
            url:`${wx.root}/banner?type=1`,
        })
       this.setData({
           swiperImage:[...res.data.banners]
       })
    },
    // 搜索
    search(e){
    },
    // 获取推荐歌单信息
    async getPlaylist(){
        const res = await wx.p.request({
            url:`${wx.root}/personalized?limit=8`
        })
        for (let index = 0; index < res.data.result.length; index++) {
            const item = res.data.result[index];
            if(item.playCount >= 10000) 
                item.count = parseInt(item.playCount/10000)+'万'
            else item.count = item.playCount+''
        }
        this.setData({
            playlist:res.data.result
        })
    },
    // 获取全部新碟信息
    async getNewAlbum(){
        const res = await wx.p.request({
            url:`${wx.root}/album/new?limit=8&area=ZH`
        })
        this.setData({
            newalbum:res.data.albums
        })
       
    },
    // 获取榜单详情
    async getToplist(){
        const res = await wx.p.request({
            url:`${wx.root}/toplist`
        })
        // console.log(res.data);
        const list = res.data.list.slice(0,3);
        for (let index = 0; index < list.length; index++) {
            const item = list[index];
            const res2 = await wx.p.request({
                url:`${wx.root}/playlist/detail?id=${item.id}`
            })
            item.songs = res2.data.privileges.slice(0,20);
        }
        for (let index = 0; index < 3; index++) {
            const item = list[index].songs;
            let s = ""
            for(let i = 0;i < 20;i++){
                if(i != 19) s += item[i].id+','
                else s += item[i].id
            }
            const res3 = await wx.p.request({
                url:`${wx.root}/song/detail?ids=${s}`
            })
            list[index].Info = res3.data.songs
        }
        this.setData({
            toplist:list
        })
    },
    // 滚动事件
    scroll1(e){
        // console.log(e.detail.scrollLeft);
        let x = parseInt(e.detail.scrollLeft/105)
        if(x > this.data.listIndex1-3)
            this.setData({
                listIndex1:x+3
            })
    },
    scroll2(e){
        // console.log(e.detail.scrollLeft);
        let x = parseInt(e.detail.scrollLeft/105)
        if(x > this.data.listIndex2-3)
            this.setData({
                listIndex2:x+3
            })
    },
    // 进入搜索页面
    GoSearch(){
        wx.navigateTo({
          url: '/pages/Search/Search',
        })
    },
    onLoad: function (options) {
        wx.showLoading({
          title: '载入中....',
        })
        this.getSwiperImage()
        this.getPlaylist()
        this.getNewAlbum()
        this.getToplist()
        const date = new Date().getDate()
        this.setData({
            today:date>=10?date:'0'+date
        })
        wx.hideLoading()
        // backgroundAudioManager.play()
    },

    onReachBottom: function () {

    },
})